package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) { //参数dishId 或setmealId  或者有
        //判断购物车是否有
        ShoppingCart shoppingCart = new ShoppingCart();
        //后续查询利用对象传进去
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查寻  select
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //判断是否为空
        if (list != null && list.size() > 0) {
            //表示有,数量加一
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            //根据购物车id修改表 update
            shoppingCartMapper.updateNumberById(shoppingCart1);
        } else {
            //没有，则插入 insert
            //先判断加入是套餐还是菜品
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId != null) {
                //为菜品,从对应的表查询数据
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //为套餐
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.inserts(shoppingCart);
        }

    }

    @Override
    public List<ShoppingCart> listCatShippingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void deleteAllShoppingCart() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteUserById(currentId);
    }

    @Override
    @Transactional
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        //后续查询利用对象传进去
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查寻  select  只会返回一个list对象
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list.get(0).getNumber() == 1){

            Long id = list.get(0).getId();
            shoppingCartMapper.deleteById(id);
        }else {
            //减一
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
            //根据购物车id修改表 update
            shoppingCartMapper.updateNumberById(shoppingCart1);
        }

    }
}
